/*
	FileName : F205_Led.c
	Description :
	Created on 2013.01.18 by ZZP.

	Revision History:

*/

#include "stm32f2xx.h"
#include "stm322xg_eval.h"

#include "F205_SysTick.h"
#include "F205_Led.h"


static __IO uint32_t WkLedTm;
static __IO uint32_t RxLedTm;
static __IO uint32_t TxLedTm;

#define LEDn	3

GPIO_TypeDef* GPIO_PORT[LEDn] = {	LED_Rx_GPIO_PORT, 	LED_Tx_GPIO_PORT, 	LED_Wk_GPIO_PORT};
const uint16_t GPIO_PIN[LEDn] = {	LED_Rx_PIN,			LED_Tx_PIN, 		LED_Wk_PIN};
const uint32_t GPIO_CLK[LEDn] = {	LED_Rx_GPIO_CLK, 	LED_Tx_GPIO_CLK, 	LED_Wk_GPIO_CLK};

#define RxLED	0
#define TxLED	1
#define WkLED	2


void STM_EVAL_LEDInit	(uint8_t Led);
void STM_EVAL_LEDOn		(uint8_t Led);
void STM_EVAL_LEDOff	(uint8_t Led);
void STM_EVAL_LEDToggle	(uint8_t Led);



///////////////////////////////////////////////////////////
///			Global function realizations
///////////////////////////////////////////////////////////


void F205_InitLED(void)
{
	STM_EVAL_LEDInit(RxLED);
    STM_EVAL_LEDInit(TxLED);
	STM_EVAL_LEDInit(WkLED);
}

void F205_PowOnLedFlash(void)
{
  	STM_EVAL_LEDOn(RxLED);
  	STM_EVAL_LEDOn(TxLED);
	STM_EVAL_LEDOn(WkLED);

  	MyInfo.SysTm = 0;
	while (MyInfo.SysTm < 500){}

  	STM_EVAL_LEDOff(RxLED);
  	STM_EVAL_LEDOff(TxLED);
	STM_EVAL_LEDOff(WkLED);

	MyInfo.SysTm = 0;
	while (MyInfo.SysTm < 300){}		
}


///////////////////////////////////////////////////////////
///			Local function realizations
///////////////////////////////////////////////////////////

//  Rx led flash
void F205_RxLed(void)
{
	if (MyInfo.RxFlag)
	{
		if (RxLedTm >= 500)
		{
			STM_EVAL_LEDOff(RxLED);
			RxLedTm = 0;
			MyInfo.RxFlag--;
		}
		else
		{
			if ((RxLedTm % 100) == 0)
			{
				STM_EVAL_LEDOn(RxLED); 			
			}
			else if ((RxLedTm % 50) == 0)
			{
				STM_EVAL_LEDOff(RxLED);
			}
			RxLedTm++;
		}
	}
	else
	{
		STM_EVAL_LEDOff(RxLED);
		RxLedTm = 0;
	}
}


// Work led flash
void F205_WkLed(void)
{
	if (WkLedTm >= 100)
	{
		STM_EVAL_LEDOn(WkLED);
		WkLedTm = 0;
	}
	else
	{
		WkLedTm++;
		if (WkLedTm == 50)
		{
			STM_EVAL_LEDOff(WkLED);
		}
	}
}

void F205_BiosLed(void)
{
	if (!MyInfo.RdyFlag)
		return;
	F205_WkLed();
	F205_RxLed();
}

/**
  * @brief  Configures LED GPIO.
  * @param  Led: Specifies the Led to be configured. 
  *   This parameter can be one of following parameters:
  *     @arg LED1
  *     @arg LED2
  *     @arg LED3
  * @retval None
  */
void STM_EVAL_LEDInit(uint8_t Led)
{
  	GPIO_InitTypeDef  GPIO_InitStructure;
  
  	/* Enable the GPIO_LED Clock */
  	RCC_AHB1PeriphClockCmd(GPIO_CLK[Led], ENABLE);


  	/* Configure the GPIO_LED pin */
  	GPIO_InitStructure.GPIO_Pin = GPIO_PIN[Led];
  	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  	GPIO_Init(GPIO_PORT[Led], &GPIO_InitStructure);
}


/**
  * @brief  Turns selected LED On.
  * @param  Led: Specifies the Led to be set on. 
  *   This parameter can be one of following parameters:
  *     @arg LED1
  *     @arg LED2
  *     @arg LED3
  * @retval None
  */
void STM_EVAL_LEDOn(uint8_t Led)
{
  	GPIO_PORT[Led]->BSRRH = GPIO_PIN[Led];
}

/**
  * @brief  Turns selected LED Off.
  * @param  Led: Specifies the Led to be set off. 
  *   This parameter can be one of following parameters:
  *     @arg LED1
  *     @arg LED2
  *     @arg LED3
  * @retval None
  */
void STM_EVAL_LEDOff(uint8_t Led)
{
  	GPIO_PORT[Led]->BSRRL = GPIO_PIN[Led]; 
}

/**
  * @brief  Toggles the selected LED.
  * @param  Led: Specifies the Led to be toggled. 
  *   This parameter can be one of following parameters:
  *     @arg LED1
  *     @arg LED2
  *     @arg LED3
  * @retval None
  */
void STM_EVAL_LEDToggle(uint8_t Led)
{
  	GPIO_PORT[Led]->ODR ^= GPIO_PIN[Led];
}








